<template>
  <div class="page_main">
    <div class="search_box">
      <el-form ref="searchForm" :inline="true" :model="searchForm" size="mini">
        <search-form-box :show-more-btn="false">
          <template slot="showSearch">
            <el-form-item label="编号" prop="code">
              <el-input v-model="searchForm.code" placeholder="请输入编号" clearable style="width: 200px"></el-input>
            </el-form-item>
						<el-form-item label="区域：" prop="adcode">
							<el-select clearable v-model="searchForm.adcode" filterable placeholder="请选择区域"
								style="width:100%">
								<el-option v-for="item in searchAreaVOList" :key="item.adcode" :label="item.area"
									:value="item.adcode">
								</el-option>
							</el-select>
						</el-form-item>
            <el-form-item label="楼盘" prop="building_area_id">
              <el-select v-model="searchForm.building_area_id" placeholder="请选择楼盘" clearable filterable
                         style="width: 200px"
              >
                <el-option v-for="item in buildingArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="媒体类型" prop="media_type_id">
              <el-select v-model="searchForm.media_type_id" placeholder="请选择媒体类型" clearable filterable
                         style="width: 200px"
              >
                <el-option v-for="item in allMediaTypeArr" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="点位状态" prop="media_point_status">
              <el-select v-model="searchForm.media_point_status" placeholder="请选择点位状态" clearable filterable
                         style="width: 200px"
              >
                <el-option v-for="item in mediaPointStatus" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button type="primary" plain @click="search">搜索</el-button>
              <el-button type="info" plain @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <el-row :gutter="20" type="flex" align="center" class="margin-bottom">
      <el-col>
        <el-button type="primary" size="mini" @click.native="showAdd()">批量设置点位规格
        </el-button>
      </el-col>
    </el-row>
    <div class="data_box">
      <el-table v-loading="tableLoading" :data="tableData" size="mini" stripe border>
        <el-table-column label="序号" fixed="left" width="50" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="媒体编号" fixed="left" prop="code" width="100" align="center"></el-table-column>
        <el-table-column label="媒体类型" prop="media_type_name" width="200" align="center"></el-table-column>
        <el-table-column label="AB面" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.ab_type_id == 57">A面</span>
            <span v-if="scope.row.ab_type_id == 58">B面</span>
          </template>
        </el-table-column>
        <el-table-column label="对外尺寸" width="200" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.size_width&&scope.row.size_height">
              {{ scope.row.size_width + 'CM*' + scope.row.size_height + 'CM' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="对内尺寸" width="200" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.size_width_inside&&scope.row.size_height_inside">
              {{ scope.row.size_width_inside + 'CM*' + scope.row.size_height_inside + 'CM' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="楼盘" prop="building_area_name" width="200" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="地址" prop="address" min-width="300" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="安装时间" prop="created_at" width="200" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" width="120" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-zoom-in" size="mini" type="primary" @click="look(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                   :current-page.sync="searchForm.page"
                   :page-sizes="[10,20,50,100, 200, 400]" :page-size.sync="searchForm.page_size"
                   layout="total, sizes, prev, pager, next, jumper" :total="total"
    >
    </el-pagination>
    <el-dialog title="查看" center width="60%" top="100px" :visible.sync="deviceDetailsVisible"
               @close="deviceDetail = {}"
    >
      <el-form label-width="120px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item v-if="deviceDetail != null" label="图片预览">
              <el-carousel v-show="(deviceDetail.imgList||[]).length>0" :interval="2000" type="card" height="200px">
                <el-carousel-item v-for="item in deviceDetail.imgList" :key="item">
                  <el-image :src="item" style="height: 200px;width: auto"></el-image>
                </el-carousel-item>
              </el-carousel>
              <el-image style="height:80px;width: 80px;margin: 10px;padding: 5px;border: 1px solid black"
                        v-show="(deviceDetail.imgList||[]).length>0" v-for="(item,index) in deviceDetail.imgList"
                        :key="index"
                        :src="item" :preview-src-list="deviceDetail.imgList"
              ></el-image>
              <div v-show="(deviceDetail.imgList||[]).length==0">暂无图片</div>
            </el-form-item>
            <el-form-item label="提交时间">
              {{ deviceDetail.created_at }}
            </el-form-item>
            <el-form-item label="备注">
              {{ deviceDetail.remark }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼盘">
              {{ deviceDetail.building_area_name }}
            </el-form-item>
            <el-form-item label="地址">
              {{ deviceDetail.address }}
            </el-form-item>
            <el-form-item label="媒体类型">
              {{ deviceDetail.media_type_name }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼号">
              {{ deviceDetail.building_number + '号楼' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="单元号">
              {{ deviceDetail.unit_number + '单元' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼层">
              {{ deviceDetail.floor + '层' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼栋类型">
              {{ deviceDetail.building_type == 1 ? '多层' : '高层' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="门号">
              {{ deviceDetail.door_number + '号门' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="AB面">
              {{ deviceDetail.ab_type_id == 57 ? 'A面' : 'B面' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="是否亮灯">
              {{ deviceDetail.is_light == 0 ? '不亮' : '亮灯' }}
            </el-form-item>
            <el-form-item label="媒体编号">
              {{ deviceDetail.code }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deviceDetailsVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

    <el-dialog title="设置点位规格" center width="30%" top="100px" :visible.sync="dialoglsVisible">
      <el-form ref="cuForm" :model="cuForm" :rules="cuRules" label-width="150px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="点位类型" prop="media_type_id">
              <el-select clearable @change="changeType" v-model="cuForm.media_type_id" placeholder="请选择点位类型"
                         style="width:100%"
              >
                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <div v-if="cuForm.media_type_id">
              <el-form-item label="画面属性" prop="pic_type_id">
                <el-select clearable v-model="cuForm.pic_type_id" placeholder="请选择画面属性" style="width:100%">
                  <el-option v-for="item in pic_types" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="对外尺寸_宽(cm)：" prop="size_width">
                <el-input clearable v-model.trim="cuForm.size_width" placeholder="请输入对外尺寸_宽"/>
              </el-form-item>

              <el-form-item label="对外尺寸_高(cm)：" prop="size_height">
                <el-input clearable v-model.trim="cuForm.size_height" placeholder="请输入对外尺寸_高"/>
              </el-form-item>

              <el-form-item label="对内尺寸_宽(cm)：" prop="size_width_inside">
                <el-input clearable v-model.trim="cuForm.size_width_inside" placeholder="请输入对内尺寸_宽"/>
              </el-form-item>

              <el-form-item label="对内尺寸_高(cm)：" prop="size_height_inside">
                <el-input clearable v-model.trim="cuForm.size_height_inside" placeholder="请输入对内尺寸_高"/>
              </el-form-item>

              <el-form-item label="规格" prop="media_type_specs_ids">
                <el-select v-model="cuForm.media_type_specs_ids"
                           placeholder="请选择规格" style="width:100%"
                >
                  <el-option v-for="item in specsList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeCU">关 闭</el-button>
        <el-button type="primary" @click.native="onAdd()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox/index'
import {
  list_all_simple,
  list_media_types,
	selectSearchAreaVO
} from '@/api/building/building'
import {
  page
} from '@/api/point/media'
import {
  type_list
} from '@/api/media/type'
import {
  media_specs_list
} from '@/api/media/specs'
import {
  edit
} from '@/api/media/relation'
import {
  list_all3rd_bycode
} from '@/api/business/setting/dict'
import common from '@/utils/common'
import {
  Message
} from 'element-ui'

export default {
  name: 'index.vue',
  components: {
    SearchFormBox
  },
  data() {
    return {
      listParams: {
        page: 1,
        page_size: 10,
        code: null,
        building_area_id: null,
        media_type_id: null
      },
      searchForm: {
        page: 1,
        page_size: 10,
        code: null,
        building_area_id: null,
        media_type_id: null,
        media_point_status: null
      },
      total: 0,
      buildingArr: [],
      allMediaTypeArr: [],
      mediaPointStatus: [
        {id: 0,name: "正常"},
        {id: 1,name: "异常"}
      ],
      typeList: [], // 点位类型列表
      specsList: [],
      inside_default: [],
      out_default: [],
      has_multiple_spec: null,
      has_distinguish_spec: null,
      tableData: [],
      tableLoading: false,
      deviceDetail: {},
      deviceDetailsVisible: false,
      dialogLoading: false,
			searchAreaVOList: [],
      cuForm: {
        pic_type_id: null,
        size_width: null,
        size_height: null,
        size_width_inside: null,
        size_height_inside: null,
        has_default: 0,
        has_inside_default: 0,
        media_type_id: null,
        media_type_specs_ids: [],
        media_type_specs_out_ids: [],
        media_type_specs_inside_ids: [],
        default_media_type_specs_id: null,
        default_media_type_specs_out_id: null,
        default_media_type_specs_inside_id: null
      },
      pic_types: [],
      cuRules: {
        media_type_id: [{
          required: true,
          message: '请选择点位类型',
          trigger: 'blur'
        }],
        media_type_specs_ids: [{
          required: true,
          message: '请选择规格',
          trigger: 'blur'
        }],
        media_type_specs_out_ids: [{
          required: true,
          message: '请选择对外规格',
          trigger: 'blur'
        }],
        media_type_specs_inside_ids: [{
          required: true,
          message: '请选择对内规格',
          trigger: 'blur'
        }],
        default_media_type_specs_out_id: [{
          required: true,
          message: '请选择默认对外规格',
          trigger: 'blur'
        }],
        default_media_type_specs_inside_id: [{
          required: true,
          message: '请选择默认对内规格',
          trigger: 'blur'
        }],
        pic_type_id: [{
          required: true,
          message: '请选择画面属性',
          trigger: 'blur'
        }],
        size_width: [{
          required: true,
          message: '对外宽不能为空',
          trigger: 'blur'
        }, {
          pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
          message: '请输入合法数字(支持2位小数)',
          trigger: 'blur'
        }],
        size_height: [{
          required: true,
          message: '对外高不能为空',
          trigger: 'blur'
        }, {
          pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
          message: '请输入合法数字(支持2位小数)',
          trigger: 'blur'
        }],
        size_width_inside: [{
          required: true,
          message: '对内宽不能为空',
          trigger: 'blur'
        }, {
          pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
          message: '请输入合法数字(支持2位小数)',
          trigger: 'blur'
        }],
        size_height_inside: [{
          required: true,
          message: '对内高不能为空',
          trigger: 'blur'
        }, {
          pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
          message: '请输入合法数字(支持2位小数)',
          trigger: 'blur'
        }]
      },
      dialoglsVisible: false
    }
  },
  created() {
    this.initALlMediaTypeArr()
    this.initBuildingArr()
    this.getList()
    this.getTypeList()
    this.getPicTypes()
		this.selectSearchArea()
  },
  methods: {
		// 获取地址分类
		selectSearchArea() {
			const that = this
			return selectSearchAreaVO()
				.then(res => {
					const {
						code,
						data
					} = res
					if (code == 200) {
						for (let i = 0; i < data.length; i++) {
							if (data[i] && data[i].adcode) {
								that.searchAreaVOList.push(data[i])
							}
						}
					}
				})
		},
    initALlMediaTypeArr() {
      list_media_types().then(res => {
        this.allMediaTypeArr = res.data
      })
    },
    initBuildingArr() {
      list_all_simple().then(res => {
        this.buildingArr = res.data
      })
    },
    getTypeList() {
      type_list().then(res => {
        this.typeList = res.data
      })
    },
    search() {
      this.searchForm.page = 1
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    getList() {
      this.tableLoading = true
      page(this.listParams).then(res => {
        this.tableLoading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    getPicTypes() {
      const that = this
      return list_all3rd_bycode({
        code: 'dwxg-hmsx'
      })
        .then(res => {
          const {
            code,
            data
          } = res
          if (code == 200) {
            that.pic_types = data
          }
        })
    },
    getSpecsList(id) {
      var obj = Object.assign({}, {
        mediaTypeId: id,
        page: 1,
        page_size: 100
      })
      media_specs_list(obj)
        .then(res => {
          const {
            code,
            message
          } = res
          if (code === 200) {
            this.specsList = res.data.records
          }

        })
        .catch(() => {

        })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    look(row) {
      this.deviceDetailsVisible = true
      this.deviceDetail = row
    },
    showAdd() {
      this.dialoglsVisible = true
    },
    changeType(val) {
      this.specsList = []
      this.cuForm.media_type_specs_ids = []
      this.cuForm.media_type_specs_out_ids = []
      this.cuForm.media_type_specs_inside_ids = []
      this.cuForm.default_media_type_specs_id = null
      this.cuForm.default_media_type_specs_out_id = null
      this.cuForm.default_media_type_specs_inside_id = null
      this.cuForm.has_default = 0
      this.cuForm.has_inside_default = 0
      this.cuForm.pic_type_id = null
      this.cuForm.size_width = null
      this.cuForm.size_height = null
      this.cuForm.size_width_inside = null
      this.cuForm.size_height_inside = null
      this.getSpecsList(val)

      this.typeList.map((item) => {
        if (item.id == val) {
          this.has_multiple_spec = item.has_multiple_spec
          this.has_distinguish_spec = item.has_distinguish_spec
        }
      })
    },
    closeCU() {
      var that = this
      that.dialoglsVisible = false
      this.cuForm = {
        media_type_id: null,
        media_type_specs_ids: [],
        media_type_specs_out_ids: [],
        media_type_specs_inside_ids: [],
        default_media_type_specs_id: null,
        default_media_type_specs_inside_id: null,
        default_media_type_specs_out_id: null,
        has_default: 0,
        has_inside_default: 0,
        pic_type_id: null,
        size_width: null,
        size_height: null,
        size_width_inside: null,
        size_height_inside: null
      }
    },

    onAdd() {
      this.$refs.cuForm.validate(valid => {
        if (valid) {
          var loading = common.showLoading('提交中')
          var obj = Object.assign({}, this.cuForm)
          // console.log(1111, obj)
          // return false
          return edit(obj)
            .then(res => {
              const {
                code,
                message
              } = res
              if (code == 200) {
                Message({
                  message,
                  type: 'success',
                  duration: 1000
                })
                this.getList()
                this.closeCU()
                loading.close()
              }
              loading.close()
            })
            .catch(() => {
              loading.close()
            })
        } else {
          // ~ 不通过
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.page_main {
  margin-top: 20px;
  padding: 0 20px;
}
</style>
